import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import swagger from './config/swagger'; //swagger文档配置
import { AllExceptionsFilter } from './common/exceptions/base.exception.filter';
import { HttpExceptionFilter } from './common/exceptions/http.exception.filter';
import { TransformInterceptor } from './common/exceptions/business.succeed';
import { NestExpressApplication } from '@nestjs/platform-express';
import { join } from 'path';
import { getConfig } from './utils';
import { ValidationPipe } from '@nestjs/common';

const { APP_SERVE } = getConfig();
async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    cors: true,
  });
  app.useGlobalInterceptors(new TransformInterceptor());
  app.useGlobalFilters(new AllExceptionsFilter(), new HttpExceptionFilter());
  // app.register(fastifyMultipart);

  // 设置静态资源访问目录
  app.useStaticAssets(join(__dirname, '..', 'static'), {
    prefix: '/files',
  });
  app.useStaticAssets(join(__dirname, '..', 'static', 'admin'), {
    prefix: '/files/admin',
  });
  swagger(app);
  app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true })); //验证/转换类型
  await app.listen(APP_SERVE.port).then(() => {
    console.log(APP_SERVE.domain + ':' + APP_SERVE.port);
  });
}
bootstrap();
